package com.likui.myqq.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.likui.myqq.R;
import com.likui.myqq.adapter.QQContactAdapter;
import com.likui.myqq.model.RecentChat;
import com.likui.myqq.task.AsyncTaskBase;
import com.likui.myqq.util.test.TestData;
import com.likui.myqq.view.CustomListView;
import com.likui.myqq.view.LoadingView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by helikui on 2015/10/4.
 */
public class QQContactFragment extends Fragment {
    private Context mContext;
    private View mBaseView;
    private CustomListView mCustomListView;
    private View mSearchView;
    private RelativeLayout mPhone;
    private LoadingView mLoadingView;
    private QQContactAdapter mAdapter;
    private List<RecentChat> users = new ArrayList<RecentChat>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContext = getActivity();
        mBaseView = inflater.inflate(R.layout.fragment_qq_contact, null);
        findView();
        init();
        return mBaseView;
    }

    private void findView() {
        mSearchView = mBaseView.findViewById(R.id.search);
        mCustomListView = (CustomListView) mBaseView.findViewById(R.id.listView);
        mPhone = (RelativeLayout) mSearchView.findViewById(R.id.rl_tonxunlu);
        mLoadingView = (LoadingView) mBaseView.findViewById(R.id.loading);
    }

    private void init() {
        mAdapter = new QQContactAdapter(mContext, users, mCustomListView);
        mCustomListView.setAdapter(mAdapter);
        mCustomListView.setCanLoadMore(false);
        mCustomListView.setCanRefresh(false);
        mPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                PhoneContactFragment phoneContactFragment = new PhoneContactFragment();
                ft.replace(R.id.rl_contact, phoneContactFragment);
                ft.setCustomAnimations(R.anim.activity_up, R.anim.fade_out);
                ft.commit();
            }
        });

        new AsyncTaskLoad(mLoadingView).execute(0);
    }


    private class AsyncTaskLoad extends AsyncTaskBase {
        List<RecentChat> rcs = new ArrayList<>();

        public AsyncTaskLoad(LoadingView loadingView) {
            super(loadingView);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(Integer integer) {
            if (integer == 1) {
                users.addAll(rcs);
                mAdapter.notifyDataSetChanged();
            }
            super.onPostExecute(integer);
        }

        @Override
        protected Integer doInBackground(Integer... params) {
            int result = -1;
            rcs = TestData.getRecentChats();
            result = 1;
            return result;
        }
    }
}
